package org.bedracket.bedracketenchantment.event;

import net.minecraft.world.entity.animal.Squid;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraftforge.event.entity.living.LivingDropsEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import org.bedracket.bedracketenchantment.init.ModEnchantments;
import org.bedracket.bedracketenchantment.util.Helpers;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE)
public class LivingEvents {

    @SubscribeEvent
    public static void onEntityDrops(LivingDropsEvent event) {
        var entity = event.getEntityLiving();
        var attacker = entity.getLastHurtByMob();

        var level = entity.getLevel();
        var pos = entity.getOnPos();
        var drops = event.getDrops();
        int looting = event.getLootingLevel();
        ItemStack stack;
        if (attacker != null) {
            var heldStack = attacker.getMainHandItem();
            int beheadingLevel = EnchantmentHelper.getItemEnchantmentLevel(ModEnchantments.BEHEADING.get(), heldStack);
            if (beheadingLevel > 0) {
                if (Math.random() <= 0.1F * beheadingLevel) {
                    switch (entity.getType().getDescriptionId()) {
                        case "entity.minecraft.skeleton" -> {
                            stack = Helpers.stack(level, Items.SKELETON_SKULL, 1, looting);
                            drops.add(Helpers.itemEntity(level, pos, stack));
                        }
                        case "entity.minecraft.creeper" -> {
                            stack = Helpers.stack(level, Items.CREEPER_HEAD, 1, looting);
                            drops.add(Helpers.itemEntity(level, pos, stack));
                        }
                        case "entity.minecraft.zombie" -> {
                            stack = Helpers.stack(level, Items.ZOMBIE_HEAD, 1, looting);
                            drops.add(Helpers.itemEntity(level, pos, stack));
                        }
                        case "entity.minecraft.wither_skeleton" -> {
                            stack = Helpers.stack(level, Items.WITHER_SKELETON_SKULL, 1, looting);
                            drops.add(Helpers.itemEntity(level, pos, stack));
                        }
                        default -> {
                        }
                    }
                }
            }
        }
    }
}
